﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using BCCommon;
using BCDto.Chats.Message;
using BCService;
using BCService.Chats;
using BCService.Common.ExceptionService;
using BCService.IM.CustomerService;

namespace PageTemplateSerializeConsole.Configs
{
    public class WebSocketHandler
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly IWebSocketContextService webSocketContextService;
        private readonly IMessageService messageService;
        private readonly ICustomerService customerService;
        private readonly ISystemExceptionRecordService systemExceptionRecordService;

        public WebSocketHandler(IApplicationContextService applicationContextService,
            IWebSocketContextService webSocketContextService,
            IMessageService messageService,
            ICustomerService customService,
            ISystemExceptionRecordService systemExceptionRecordService)
        {
            this.applicationContextService = applicationContextService;
            this.webSocketContextService = webSocketContextService;
            this.messageService = messageService;
            this.customerService = customService;
            this.systemExceptionRecordService = systemExceptionRecordService;

            this.webSocketContextService.OnConnected += this.OnConnected;
            this.webSocketContextService.OnTextReceived += this.OnTextReceived;
            this.webSocketContextService.OnBinaryReceived += this.OnBinaryReceived;
            this.webSocketContextService.OnClosed += this.OnClosed;
        }

        public async Task ConnectAsync()
        {
            await this.webSocketContextService.TryListenAsync();
        }

        private void OnConnected()
        {
            if (this.applicationContextService.Admin != null)
            {
                this.customerService.Connect();
            }

            if (applicationContextService.User != null)
            {
                // TODO: 待实现。
            }
        }

        private void OnTextReceived(string text)
        {
            try
            {
                var requestDto = JsonConvert.DeserializeObject<MessageRequestDto>(text, GeneralContainer.JsonSerializerSettings);

                this.messageService.Create(requestDto);
            }
            catch (ArgumentException ex)
            {
                this.webSocketContextService.SendTextAsync(ex.Message);
            }
            catch (Exception ex)
            {
                this.systemExceptionRecordService.InsertException(ex, string.Empty);
            }
        }

        private void OnBinaryReceived(byte[] bytes)
        {
            // TODO: 待实现。
        }

        private void OnClosed()
        {
            if (this.applicationContextService.Admin != null)
            {
                this.customerService.Disconnect();
            }

            if (applicationContextService.User != null)
            {
                // TODO: 待实现。
            }
        }
    }
}
